package com.cote.douhahou.pretloan.any.data.akomapalendtask;

import android.content.Context;
import android.text.TextUtils;

import com.cote.douhahou.pretloan.any.data.akomapalendhttp.AkomapalendASReqstManger;
import com.cote.douhahou.pretloan.any.data.akomapalendlogger.AkomapalendASLger;
import com.cote.douhahou.pretloan.any.data.akomapalendservices.AkomapalendASBtryIfo;
import com.cote.douhahou.pretloan.beans.AkomapalendASRequestPms;
import com.cote.douhahou.pretloan.url.AkomapalendPaths;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class AkomapalendASDevcBtyTsk implements Supplier<String> {

    private final static String TAG = "ASDeviceBatteryTask";

    private final String mTaskId;
    private final Context mContext;

    public AkomapalendASDevcBtyTsk(Context context, String taskId) {
        this.mContext = context;
        this.mTaskId = taskId;
    }

    @Override
    public String get() {
        AkomapalendASLger.logd(TAG, TAG + " Processing...");

        if (TextUtils.isEmpty(AkomapalendPaths.PATH_BATTERY)) {
            return mTaskId + "";
        }

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CountDownLatch latch = new CountDownLatch(1);
        AsyncBtyTsk asyncTask = new AsyncBtyTsk(mContext, latch, mTaskId);
        executorService.submit(asyncTask);

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return mTaskId + "";
    }

    static class AsyncBtyTsk implements Runnable {
        private final CountDownLatch latch;
        private final Context mContext;
        private final String mTaskId;

        AsyncBtyTsk(Context context, CountDownLatch latch, String taskId) {
            this.mContext = context;
            this.latch = latch;
            this.mTaskId = taskId;
        }

        @Override
        public void run() {
            AkomapalendASBtryIfo batteryInfo = new AkomapalendASBtryIfo();
            batteryInfo.btyInfo(mContext, batteryInfoBean -> {
                AkomapalendASRequestPms netParams = new AkomapalendASRequestPms();
                netParams.stDB(batteryInfoBean);
//                String result = new Gson().toJson(netParams).trim();
                String result = netParams.toEnptParam(AkomapalendPaths.PATH_BATTERY);

                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                    String url = AkomapaLendUtil.commonUrl();
                    new AkomapalendASReqstManger.Builder().stReqId(this.mTaskId).stUrl(url).seBody(result).build().doPtRqst();

                    return mTaskId + "";
                });

                future.thenRun(latch::countDown);
            });

        }
    }

}
